<template>
	<view>
		<!-- <j-cell-group title="基础设置">
		    <j-cell title="通知开关" :more="false">
				<view slot="tips" style="margin-right:20rpx;">
					<switch :checked="door_lock_subscri" color="#FF0025" style="transform:scale(0.8)" @change="switchChange"></switch>
				</view>
			</j-cell>
			<j-cell title="修改名称" :tips="device.deviceName"  @jftap="openPopup"></j-cell>
			<j-cell title="分享" :to="'/jvss/pagesDevice/device/device-live/door-lock/share?token=' + device.token"></j-cell>
		</j-cell-group> -->
		<!-- <j-cell-group title="云服务">
		    <j-cell title="云存储" :to="'/jvss/pagesDevice/device/device-live/door-lock/msgDetail?token=' + device.token + '&deviceName=' + device.deviceName"></j-cell>
		</j-cell-group>
		<j-cell-group title="其他">
		    <j-cell title="固件升级" :to="''"></j-cell>
		    <j-cell
		        title="动态密码"
		        :to="'/jvss/pagesDevice/device/device-live/door-lock/password?device=' + JSON.stringify(device)"
		    ></j-cell>
		</j-cell-group> -->
		<j-cell-group>
			<!-- <j-cell title="通知开关" :more="false">
				<view slot="tips" style="margin-right:20rpx;">
					<switch :checked="door_lock_subscri" color="#FF0025" style="transform:scale(0.8)"
						@change="switchChange"></switch>
				</view>
			</j-cell> -->
			<!-- <j-cell title="基本设置" @jftap="settingFunc('basicSetup')"></j-cell> -->
			<j-cell title="基本设置" @jftap="goSetting('basicSetup',0)"></j-cell>
			<!-- <j-cell title="门锁设置"
				v-if="doorLockSetting"
				:to="'/jvss/pagesDevice/device/device-live/door-lock/setting/lockSetting//doorlockSetting?token=' + device.token + '&deviceName=' + device.deviceName + '&deviceNo=' + device.deviceNo">
			</j-cell> -->
			<j-cell title="门锁设置" v-if="doorLockSetting" @jftap="goSetting('doorlockSetting',1)">
			</j-cell>
			<!-- <j-cell title="密码权限" @jftap="settingFunc('pwperMission')"></j-cell> -->
			<!-- <j-cell title="密码权限"
				:to="'/jvss/pagesDevice/device/device-live/setting/password?deviceNo=' + device.deviceNo + '&devUserName=' + device.devUserName + '&token=' + device.token+ '&devPassWord=' + device.devPassWord">
			</j-cell> -->
		</j-cell-group>
		<j-cell-group>
			<!-- @jftap="settingFunc('storageManager')" -->
			<!-- <j-cell title="储存管理" :to="'/jvss/pagesDevice/device/device-live/setting/diskmemoro?token=' + device.token">
			</j-cell> -->
			<j-cell title="储存管理" @jftap="goSetting('diskmemoro',2)">
			</j-cell>
		</j-cell-group>
		<j-cell-group>
			<!-- <j-cell title="高级设置"  @jftap="settingFunc('advancedSetup')"></j-cell> -->
			<!-- <j-cell title="关于设备" @jftap="settingFunc('aboutDevice')"></j-cell> -->
			<j-cell title="关于设备" @jftap="goSetting('aboutDevice',3)"></j-cell>
		</j-cell-group>
		<view class="u-f-ajc bottm" @click="removeDevice">移除设备</view>
		<uni-popup ref="inputDialog" type="dialog" :is-mask-click="false" class="popupName" @maskClick="maskClick">
			<view class="form">
				<view class="u-f-ajc title">修改名称</view>
				<view class="form-item">
					<!-- <input
						:focus="focus"
						placeholder="请输入设备名称"
						name="deviceName"
						v-model="device.deviceName"
					/> -->
					<uni-easyinput v-model="editDeviceName" placeholder="请输入设备名称"></uni-easyinput>
				</view>
				<view class="u-f-r form-item">
					<view class="u-f-ajc cancel" @click="close">取消</view>
					<view class="u-f-ajc conf" @click="submit">保存</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		settingMixins
	} from '../mixins/setting.js'
	const constant = getApp().globalData.constant.DEVICE_TYPE;
	export default {
		mixins: [settingMixins],
		components: {},
		data() {
			return {
				doorLockSetting: false, //博客门锁设置
			};
		},
		onLoad(opt) {
			let pid = this.device.pid
			if (pid == constant.EE_DEV_SMARTDOORLOCK8 || pid == constant.EE_DEV_SMARTDOORLOCK3) {
				this.doorLockSetting = true
			}
		},
		methods: {
			maskClick() {
				this.editDeviceName = ''
				this.$refs.inputDialog.close()
			},
			goSetting(type, index) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				console.log(type, index)
				//基本设置 门锁设置 存储管理 关于设备  
				let urlList = [
					`${'./'+type}?device=${encodeURIComponent(JSON.stringify(this.device))}`,
					'/jvss/pagesDevice/device/device-live/door-lock/setting/lockSetting/doorlockSetting?token=' + this
					.device.token + '&deviceName=' + this.device.deviceName + '&deviceNo=' + this.device.deviceNo,
					'/jvss/pagesDevice/device/device-live/setting/diskmemoro?token=' + this.device.token,
					`${'./'+type}?device=${encodeURIComponent(JSON.stringify(this.device))}&deviceType=AHD`
				]
				let url = urlList[index]
				const data = {
					token: [this.device.token]
				};
				getApp().globalData.api.getDeviceStatus(data, (result) => {
					setTimeout(() => {
						uni.hideLoading()
					}, 100)
					console.log('设置跳转页获取设备状态==', result);
					this.$logs({
						info: '设置页跳转获取设备状态',
						sn: this.device.deviceNo,
						result: result,
						cacheData: uni.getStorageSync(this.device.deviceNo)
					})
					let item = result[0]
					this.device.status = item
					if (item.status === 'online') {
						if (item.wakeUpStatus === '0') {
							if (item.wakeUpEnable === '0') {
								let cacheData = uni.getStorageSync(this.device.deviceNo)
								if (index == 0) {
									if (cacheData.volume) {
										uni.navigateTo({
											url: url
										})
									} else {
										uni.showToast({
											title: '设备处于深度休眠，请手动唤醒',
											icon: 'none'
										})
									}
								}
								if (index == 3) {
									if (cacheData.info) {
										uni.navigateTo({
											url: url
										})
									} else {
										uni.showToast({
											title: '设备处于深度休眠，请手动唤醒',
											icon: 'none'
										})
									}
								}
								//深度休眠 手动唤醒
								uni.showToast({
									title: '设备处于深度休眠，请手动唤醒',
									icon: 'none'
								})
							} else {
								//浅休眠 可以唤醒
								// this.getAdminToken(url)
								if (uni.getStorageSync(this.device.deviceNo)) {
									let cacheData = uni.getStorageSync(this.device.deviceNo)
									if (index == 0) {
										if (cacheData.volume) {
											uni.navigateTo({
												url: url
											})
										} else {
											this.getAdminToken(url)
										}
									} else if (index == 3) {
										if (cacheData.info) {
											uni.navigateTo({
												url: url
											})
										} else {
											this.getAdminToken(url)
										}
									} else {
										this.getAdminToken(url)
									}
								} else {
									uni.navigateTo({
										url: url
									})
								}
							}
						} else if (item.wakeUpStatus === '1') {
							//已唤醒 在线
							uni.navigateTo({
								url: url
							})
						} else if (item.wakeUpStatus === '2') {
							// 准备休眠中
							uni.showToast({
								title: '设备休眠中，请稍后唤醒',
								icon: 'none'
							})
						}
					} else {
						//离线
						uni.showToast({
							title: '设备已离线',
							icon: 'none'
						})
						uni.navigateBack({
							delta: 2
						})
					}
				})
			},
			// 获取设备最新的adminToken
			getAdminToken(url) {
				let data = {
					deviceNo: this.device.deviceNo,
					isLatest: true
				}
				getApp().globalData.api.getDeviceTokenLocal(data, (result) => {
					this.device.adminToken = result.adminToken
					this.loginDevice(url)
				});
			},
			//登录设备
			loginDevice(url) {
				let token = this.device.token
				let data = {
					encryptType: 'DISABLE',
					LoginType: 'DVRIP-Web',
					UserName: this.device.devUserName,
					PassWord: this.device.devPassWord,
					Name: 'generalinfo',
					KeepaliveTime: 60
				};
				if (this.device.adminToken) {
					data.LoginToken = this.device.adminToken
					data.encryptType = 'TOKEN'
				}
				getApp().globalData.api.deviceLogin(data, token, (result) => {
					console.log('login 成功', result);
					uni.navigateTo({
						url: url
					})
				})
			}
		}
	};
</script>
<style lang="scss">
	.popupName {
		/deep/ .uni-popup__wrapper {
			top: -100rpx;
		}
	}

	.bottm {
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 40rpx;
		height: 84rpx;
		background: rgba(255, 255, 255, 0.39);
		border: 1rpx solid #B9B9B9;
		border-radius: 15rpx;
		ont-size: 30rpx;
		color: #FF0025;
		font-weight: 700;
	}

	.form {
		background: white;
		padding: 60rpx;
		border-radius: 10rpx;

		.title {
			font-size: 34rpx;
			font-weight: bold;
			line-height: 49rpx;
			color: #464646;
		}

		.form-item {
			margin-top: 40rpx;

			// input{
			// 	border: 1rpx solid #A5A5A5;
			// 	border-radius: 8rpx;
			// 	padding: 10rpx;
			// }
			.cancel {
				background: #F6F7FB;
				color: #000000;
				font-size: 30rpx;
				width: 237rpx;
				height: 84rpx;
				border-radius: 42rpx;
			}

			.conf {
				margin-left: 20rpx;
				background: #FF0025;
				color: #ffffff;
				font-size: 30rpx;
				width: 237rpx;
				height: 84rpx;
				border-radius: 42rpx;
			}
		}

		.form-title {
			text-align: center;
			margin-bottom: 40rpx;
			font-weight: bold;
		}
	}
</style>